package com.sosee.cgfw.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sosee.cgfw.R;
import com.sosee.cgfw.adapter.FashionAdapter;
import com.sosee.cgfw.entity.News;
import com.sosee.cgfw.entity.ad.ADJson;
import com.sosee.cgfw.service.ExampleService;
import com.sosee.cgfw.utils.Constans;
import com.sosee.cgfw.utils.LogUtils;
import com.sosee.cgfw.utils.Utils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.analytics.MobclickAgent;

import org.com.cctest.view.XListView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import scrollimage.common.ScrollImageEntity;
import scrollimage.image.ImageScroll;
import scrollimage.image.ScrollImageController;

/**
 * Created by Thinkpad on 2016/1/25.
 * 新闻列表activity
 */
public class FashionNewsActivity extends Activity implements XListView.IXListViewListener, AdapterView.OnItemClickListener {
    private List<News> list;//新闻实体集合
    private HttpUtils httpUtils;
    private XListView xlv;//新闻列表
    private FashionAdapter adapter;//适配器
    private int page=0;
    private View headView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fashionnews);
        getAd();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
    private List<ADJson> aList=new ArrayList<ADJson>();
    //获取广告信息
    public void getAd(){
        httpUtils=new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.GET, Constans.NEWS_URL, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                aList=JSONArray.parseArray(responseInfo.result,ADJson.class);
                initUI();
                initData();
            }

            @Override
            public void onFailure(HttpException e, String s) {
                initUI();
                initData();
            }
        });
    }


    //初始化界面
    //初始化界面
    public void initUI(){
        list=new ArrayList<News>();
        xlv= (XListView) findViewById(R.id.xlv);
        headView =View.inflate(this,R.layout.ad_scroll_image_copy,null);
        initScrollView();
        initData_view();
        xlv.addHeaderView(headView);
        adapter=new FashionAdapter(this);
        adapter.setList(list);
        if(xlv==null){
            Utils.showToast("xlistview==null");

        }else{
            xlv.setPullLoadEnable(true);
            xlv.setPullRefreshEnable(true);
            xlv.setXListViewListener(this);
            xlv.setAdapter(adapter);
            xlv.setOnItemClickListener(this);
        }

    }
    //自定义 时间比较器
    private Comparator myCompartor=new Comparator<News>() {
        @Override
        public int compare(News lhs, News rhs) {
            return rhs.getDate().compareTo(lhs.getDate());
        }
    };
    //获取网络新闻资讯列表
    public void initData(){
        int begin;
        int end;
        begin=page*10;
        end=page*10+9;
        httpUtils.configCurrentHttpCacheExpiry(10);

        httpUtils.send(HttpRequest.HttpMethod.GET, Constans.URL_NEWS+"?rowstart="+begin+"&rowend="+end, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                List<News> temlist
                =Utils.getNewsFromJson(responseInfo.result);
                Collections.sort(temlist,myCompartor);
                list.addAll(temlist);
                //刷新listview
                adapter.setList(list);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Utils.showToast(getString(R.string.internet_error));
            }
        });
    }

    @Override
    public void onRefresh() {
        //下拉刷新
        initData();
        xlv.stopRefresh();
    }

    @Override
    public void onLoadMore() {
        page++;
        initData();
        xlv.stopLoadMore();
        //不实现
    }
    //item点击事件
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        News news= (News) adapterView.getItemAtPosition(i);
        //跳转activity
        Intent intent=new Intent(this,NewWebActivity.class);
        LogUtils.log_E("测试新闻网页链接");
        intent.putExtra("url",news.getLink());
        intent.putExtra("title",news.getTitle());
        intent.putExtra("text","");
        intent.putExtra("imageurl",news.getPic());
        startActivity(intent);
    }
    //回退按钮
    public void back(View view){
        finish();
    }



    ImageScroll myPager; // 图片容器
    LinearLayout ovalLayout; // 圆点容器

    private ScrollImageController scrollImageView;
    private List<ScrollImageEntity> list_view;


    /**
     * 初始化滚动图片布局
     */
    private void initScrollView() {
        myPager = (ImageScroll) headView.findViewById(R.id.myvp);
        ovalLayout = (LinearLayout) headView.findViewById(R.id.vb);
    }

    /**
     * 初始化滚动图片数据数据
     */
    private void initData_view() {

//        startService(new Intent(this, ExampleService.class));
        scrollImageView = new ScrollImageController(this);
        list_view = new ArrayList<ScrollImageEntity>();
        if(aList.size()>0){
            for (int i=0;i<aList.size();i++){
                ScrollImageEntity scrollImageEntity1 = new ScrollImageEntity();
                scrollImageEntity1.setTitle(aList.get(i).getData().getName());
                String url=Constans.BASE_URL.substring(0, Constans.BASE_URL.length()-1)+aList.get(i).getData().getPic();
                scrollImageEntity1.setImageUrl(url);
                scrollImageEntity1.setUrl(aList.get(i).getData().getLink());
                list_view.add(scrollImageEntity1);
                LogUtils.log_E("测试广告url："+url);
            }
        }
        //启用图片滚动
        scrollImageView.initViewPagerImage(list_view, myPager, ovalLayout,true);
    }



}
